package com.oss.config;

import com.oss.entity.system.User;
import lombok.extern.slf4j.Slf4j;

/**
 * 全局上下文
 * @author chengqiang
 */
@Slf4j
public class ThreadContext {
    private static final ThreadLocal<User> AUTHORIZE_INFO = new ThreadLocal<>();

    public static void setAuthorizeInfo(User value) {
        if (value == null) {
            deleteAuthorizeInfo();
        } else {
            AUTHORIZE_INFO.set(value);

        }
    }
    public static void deleteAll() {
        AUTHORIZE_INFO.remove();
    }
    public static void deleteAuthorizeInfo() {
        AUTHORIZE_INFO.remove();
    }


    /**
     * 获取用户登录信息
     * @return 用户登录信息
     */
    public static User getAuthorizeInfo() {
        return AUTHORIZE_INFO.get();
    }

    /**
     * 获取当前登录人ID
     * @return 返回用户ID
     */
    public static String getUserId(){
        User user = getAuthorizeInfo();
        String userID = "1";
        if (user != null) {
            userID = user.getUserId();
        }
        return userID;
    }
}
